﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.ZGCG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 中国成果操作对象
    /// </summary>
    private ZGCG_DAO zgcgDAO = new ZGCG_DAO();
    /// <summary>
    /// 日志操作对象
    /// </summary>
    private OperationLog_DAO operationLogDAO = new OperationLog_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取专业分类
        String zyfl = Request["zyfl"];
        //年份
        String nf = Request["nf"];

        User user = (User)Session["login"];
        //登陆超时则退到登录页
        if (null == user)
        {
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }

        ZGCG_FL zgcgFL = null;
        //验证年份
        if (null == nf || nf.Length != 4)
        {
            message = "错误：年份信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        if (null == zyfl || "".Equals(zyfl))
        {
            message = "错误：专业分类为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            zgcgFL = zgcgDAO.GetZGCG_FLByZyfl(zyfl);
            if (null == zgcgFL)
            {
                message = "错误：没有找到对应的专业分类";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalZGCG.LISTPM_PAGE + "?catalog=" + zyfl;

        //专业分类
        this.zyflTextBox.Text = zgcgFL.zyfl;
        //分类名称
        this.flmcTextBox.Text = zgcgFL.flmc;
        //分类编码
        this.flbmTextBox.Text= "ZK" + zgcgFL.flbm;
        //发布年份
        this.fbnfTextBox.Text = nf;
        //成果编号
        this.cgbhTextBox.Text = "ZK-" + nf;

        //成果名称
        this.cgmcTextBox.Text = "";
        //完成单位
        this.wcdwTextBox.Text = "";
        //完成人
        this.wcrTextBox.Text = "";
        //中文主题词
        this.zwztcTextBox.Text = "";
        this.dwdzTextBox.Text = "";//单位地址
        this.yzbmTextBox.Text = "";//邮政编码
        this.dwdhTextBox.Text = "";//单位电话
        //成果简介
        this.cgjjTextBox.Text = "";
        //研究起止时间
        this.yjqzsjTextBox.Text = "";
        this.gtflTextBox.Text = "";//国图分类
        this.cglbTextBox.Text = "";//成果类别
        this.cgspTextBox.Text = "";//成果水平
        this.jdbmTextBox.Text = "";//鉴定部门
        this.jdrqTextBox.Text = "";//鉴定日期
        this.lxhhjTextBox.Text = "";//立项或获奖
        this.fbdwTextBox.Text = "";//发布单位
        this.zllyTextBox.Text = "";//资料来源
        this.lybhTextBox.Text = "";//来源编号
        this.hydmTextBox.Text = ""; //行业代码
        this.hymcTextBox.Text = ""; //行业名称

        this.zlsqTextBox.Text = "";//专利授权
   }

    /// <summary>
    /// 获取用户填写的中国成果篇名信息
    /// </summary>
    /// <returns></returns>
    private ZGCG_PM GetZGCGPM()
    {
        ZGCG_PM zgcgPM = new ZGCG_PM();
        //成果编号
        zgcgPM.cgbh = this.cgbhTextBox.Text;
        //分类编码
        zgcgPM.flbm = this.flbmTextBox.Text;
        //成果名称
        zgcgPM.cgmc = this.cgmcTextBox.Text;
        //完成单位
        zgcgPM.wcdw = this.wcdwTextBox.Text.Trim();
        //完成人
        zgcgPM.wcr = this.wcrTextBox.Text.Trim();
        //单位地址
        zgcgPM.dwdz = this.dwdzTextBox.Text.Trim();
        //邮政编码
        zgcgPM.yzbm = this.yzbmTextBox.Text;
        //单位电话
        zgcgPM.dwdh = this.dwdhTextBox.Text;
        //中文主题词
        zgcgPM.zwztc = this.zwztcTextBox.Text;
        //成果简介
        zgcgPM.cgjj = this.cgjjTextBox.Text;
        zgcgPM.yjqzsj = this.yjqzsjTextBox.Text;//研究起止时间
        zgcgPM.gtfl = this.gtflTextBox.Text;//国图分类
        zgcgPM.cglb = this.cglbTextBox.Text;//成果类别
        zgcgPM.cgsp = this.cgspTextBox.Text;//成果水平

        zgcgPM.jdbm = this.jdbmTextBox.Text;//鉴定部门
        zgcgPM.jdrq = this.jdrqTextBox.Text;//鉴定日期
        zgcgPM.lxhhj = this.lxhhjTextBox.Text;//立项或获奖
        zgcgPM.fbdw = this.fbdwTextBox.Text;//发布单位
        zgcgPM.zlly = this.zllyTextBox.Text;//资料来源
        zgcgPM.fbnf = Int32.Parse(this.fbnfTextBox.Text);//发布年份
        zgcgPM.lybh = this.lybhTextBox.Text;//来源编号
        zgcgPM.hydm = this.hydmTextBox.Text;//行业代码
        zgcgPM.hymc = this.hymcTextBox.Text;//行业名称
        zgcgPM.zysq = this.zlsqTextBox.Text;//专利授权
        zgcgPM.kf = this.kfCheckBox.Checked;//开放
        zgcgPM.gxrq = DateTime.Now;//更新日期

        return zgcgPM;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //验证成果编号
        if (!this.ValidateCGBH())
        {
            return;
        }

        //验证成果名称
        if (!this.ValidateCGMC())
        {
            return;
        }
        //查重
        if (!this.ValidateCgmcAndFbnf())
        {
            return;
        }

        //验证中文主题词
        if (!this.ValidateZWZTC())
        {
            return;
        }

        ZGCG_PM zgcgPM = this.GetZGCGPM();
        if (this.zgcgDAO.InsertZGCG_PM(zgcgPM))
        {
            Response.Write("<Script>alert('添加中国成果信息成功')</Script>");
            this.BindData();
        }
        else
        {
            Response.Write("<Script>alert('添加中国成果信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证成果编号
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCGBH()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (this.cgbhTextBox.Text.Split('-')[1] != this.fbnfTextBox.Text)
        {
            this.errorLabel.Text = "错误：成果编号中的年份与发布年份不符.";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证成果名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCGMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.cgmcTextBox.Text||"".Equals(this.cgmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：中文名称不能为空";
            flag = false;
        }
        return flag;
    }

    //查重
    private Boolean ValidateCgmcAndFbnf()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        ZGCG_PM zgcgPM = this.zgcgDAO.GetZGCG_PMByCgmcAndFbnf(this.cgmcTextBox.Text, Int32.Parse(this.fbnfTextBox.Text));
        if (null != zgcgPM)
        {
            this.errorLabel.Text = "错误: 同一名称和发布年份的成果已经存在";
            flag = false;
        }

        return flag;
    }

    ///<summary>验证中文主题词</summary>
    private Boolean ValidateZWZTC()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";

        if (null == this.zwztcTextBox.Text || "".Equals(this.zwztcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误: 中文主题词不能为空";
            flag = false;
        }
        else
        {
            String[] p = this.zwztcTextBox.Text.Split(';');
            if (p.Length < 3)
            {
                this.errorLabel.Text = "错误: 中文主题词不能少于3个";
                flag = false;
            }
        }

        return flag;
    }

    /// <summary>验证日期</summary>
    private Boolean ValidateRQ(String rq, String msg)
    {
        this.errorLabel.Text = "";

        Boolean flag = true;
        if (rq != "")
        {
            try
            {
                DateTime val = DateTime.Parse(rq);
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：" + msg + "格式不正确(yyyy-MM-dd).";
                return false;
            }
        }
        return flag;
    }
   
    //全角转半角
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    protected void gtflTextBox_TextChanged(object sender, EventArgs e)
    {
        this.gtflTextBox.Text = GlobalMethod.Semicolon(this.gtflTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void hymcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.hymcTextBox.Text = GlobalMethod.Semicolon(this.hymcTextBox.Text.Trim());
    }
    protected void hydmTextBox_TextChanged(object sender, EventArgs e)
    {
        this.hydmTextBox.Text = GlobalMethod.Semicolon(this.hydmTextBox.Text.Trim());
    }
    protected void wcrTextBox_TextChanged(object sender, EventArgs e)
    {
        this.wcrTextBox.Text = GlobalMethod.Semicolon(this.wcrTextBox.Text.Trim());
    }
    protected void flbmTextBox_TextChanged(object sender, EventArgs e)
    {
        this.flbmTextBox.Text = GlobalMethod.Semicolon(this.flbmTextBox.Text.Trim());
    }
}